package com.luna.servlets;

import com.luna.beans.Phase;
import com.luna.beans.User;
import com.luna.service.PhaseService;
import com.luna.serviceImpl.PhaseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(
        name = "PhaseAddition",
        urlPatterns = "/PhaseAddition"
)
public class PhaseAddition extends HttpServlet {

    public PhaseAddition() {}

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int book_id = Integer.parseInt(req.getParameter("book_id"));
        PhaseService phaseService = new PhaseServiceImpl();
        PrintWriter printWriter = resp.getWriter();
        Phase phase = new Phase(
                req.getParameter("phase-name"),
                req.getParameter("phase-content"),
                book_id
        );
        if (phaseService.addPhase(phase)) {
            printWriter.print("true");
        } else {
            printWriter.print("false");
        }
        printWriter.close();
    }
}
